package com.swj.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swj.common.core.model.SysOperaLog;
import com.swj.common.core.utils.SysStringUtil;
import com.swj.modules.system.mapper.SysOperaLogMapper;
import com.swj.modules.system.model.SysOperaLogQueryVO;
import com.swj.modules.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Primary
public class SysOperLogServiceImpl
        implements ISysOperLogService {

    @Autowired
    private SysOperaLogMapper operaLogMapper;

    @Override
    public void save(SysOperaLog operLog) {
        operaLogMapper.insert(operLog);

    }

//    /**
//     * 新增操作日志
//     *
//     * @param operLog 操作日志对象
//     * @return 结果
//     */
//    @Override
//    public int insertOperlog(SysOperaLog operLog) {
//        return operaLogMapper.insertOperlog(operLog);
//    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperaLog> selectOperLogList(SysOperaLogQueryVO operLog) {
        return operaLogMapper.selectList(getOperaLogWrapper(operLog));
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operaLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public Page<SysOperaLog> selectOperLogPage(SysOperaLogQueryVO operaLog) {
        Page<SysOperaLog> page = new Page<>(operaLog.getPageNum(), operaLog.getPageSize());
        return operaLogMapper.selectPage(page,getOperaLogWrapper(operaLog));
    }

    private QueryWrapper<SysOperaLog> getOperaLogWrapper(SysOperaLogQueryVO operaLog){
        QueryWrapper<SysOperaLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(SysStringUtil.isNotEmpty(operaLog.getBusinessTypes()),SysOperaLog::getTitle,operaLog.getTitle())
                .in(SysStringUtil.isNotEmpty(operaLog.getBusinessTypes()),SysOperaLog::getBusinessTypes,operaLog.getBusinessTypes())
                .like(SysStringUtil.isNotEmpty(operaLog.getOperName()),SysOperaLog::getOperName,operaLog.getOperName())
                .eq(Objects.nonNull(operaLog.getStatus()),SysOperaLog::getStatus,operaLog.getStatus())
                .gt(Objects.nonNull(operaLog.getBeginTime()), SysOperaLog::getCreateTime, operaLog.getBeginTime())
                .lt(Objects.nonNull(operaLog.getEndTime()), SysOperaLog::getCreateTime, operaLog.getEndTime());
        return wrapper;
    }

    //
//    /**
//     * 批量删除系统操作日志
//     *
//     * @param operIds 需要删除的操作日志ID
//     * @return 结果
//     */
//    @Override
//    public int deleteOperLogByIds(Long[] operIds) {
//        return operaLogMapper.deleteOperLogByIds(operIds);
//    }
//
//    /**
//     * 查询操作日志详细
//     *
//     * @param operId 操作ID
//     * @return 操作日志对象
//     */
//    @Override
//    public SysOperaLog selectOperLogById(Long operId) {
//        return operaLogMapper.selectOperLogById(operId);
//    }
//
//    /**
//     * 清空操作日志
//     */
//    @Override
//    public void cleanOperLog() {
//        operaLogMapper.cleanOperLog();
//    }
}
